package fsnotifyalter

import (
	"io/ioutil"

	"github.com/CodyGuo/dingtalk"
	"go.uber.org/zap"
)

type Alter interface {
	send(title, message string)
}

// 钉钉告警配置
type DingTalk struct {
	WebHook string
	Secret  string
}

// email告警配置, 暂不支持
type Email struct{}

// Wechat告警配置, 暂不支持
type Wechat struct{}

func NewDingTalk(webhook, secret string) *DingTalk {
	return &DingTalk{
		WebHook: webhook,
		Secret:  secret,
	}
}

func NewEmail() *Email {
	return &Email{}
}

func NewWechat() *Wechat {
	return &Wechat{}
}

func (d DingTalk) send(title, message string) {
	dt := dingtalk.New(d.WebHook, dingtalk.WithSecret(d.Secret))

	// markdown类型
	if err := dt.RobotSendMarkdown(title, message); err != nil {
		e := err.(*dingtalk.Error)
		logger.Panic("钉钉发送消息失败", zap.String("error", e.Unwrap().Error()), zap.String("title", title), zap.String("message", message))
	} else {
		response, _ := dt.GetResponse()
		reqBody, _ := response.Request.GetBody()
		reqData, _ := ioutil.ReadAll(reqBody)
		logger.Info("钉钉发送消息成功", zap.String("message", string(reqData)))
	}
}

func (w Wechat) send(title, message string) {

}

func (e Email) send(title, message string) {

}
